﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XDL.Framework.Screens;
using Microsoft.Xna.Framework;
using XDL.Framework.Input;
using JumpNFight.Simulation;
using XDL.Framework;

namespace JumpNFight.Screens
{
    class OptionsPopup : DialogBoxScreen
    {
        AudioManager m_audio;
        MenuItem m_musicVolumeMenu;

        public OptionsPopup()
        {
            m_audio = GameServices.GetService<AudioManager>();

            // displays on top of the main game screen
            IsPopup = true;

            TransitionOnTime = TimeSpan.FromSeconds(0.25f);
            TransitionOffTime = TimeSpan.FromSeconds(0.25f);
            TextColor = Color.White;

            TextBlock titleItem = new TextBlock("OPTIONS");
            titleItem.TextAlignment = TextElement.TextAlign.Center;
            titleItem.FontName = "Fonts/PopupHeader";
            titleItem.Scale = 2;
            Controls.Add(titleItem);
            Separator separatorItem = new Separator("Separator");
            Controls.Add(separatorItem);

            for (int i = 0; i < World.MaxPlayers; i++)
            {
                IndexedMenuItem menuEntry = new IndexedMenuItem((PlayerIndex)i, string.Format("Player {0} Input: {1}", i + 1, InputToString(AnyInputState.InputDeviceTypes[i])));
                menuEntry.Scale = 2;
                menuEntry.TextAlignment = TextElement.TextAlign.Left;
                menuEntry.Selected += new EventHandler<EventArgs>(ChangePlayerInput);
                Controls.Add(menuEntry);
            }

            m_musicVolumeMenu = new MenuItem(string.Format("Music volume: {0}%", m_audio.MusicVolume));
            m_musicVolumeMenu.Scale = 2;
            m_musicVolumeMenu.TextAlignment = TextElement.TextAlign.Left;
            m_musicVolumeMenu.Selected += new EventHandler<EventArgs>(musicVolumeMenu_Selected);
            Controls.Add(m_musicVolumeMenu);

            MenuItem exitToMenu = new MenuItem("Exit To Menu");
            exitToMenu.Scale = 2;
            exitToMenu.TextAlignment = TextElement.TextAlign.Right;
            exitToMenu.Selected += (o, e) => { ExitScreen(false); };
            Controls.Add(exitToMenu);
        }

        void musicVolumeMenu_Selected(object sender, EventArgs e)
        {
            int v = m_audio.MusicVolume;
            v += 10;
            if (v > 100)
                v = 0;
            m_audio.MusicVolume = v;
            m_musicVolumeMenu.Text = string.Format("Music volume: {0}%", m_audio.MusicVolume);
        }

        string InputToString(AnyInputState.InputDeviceType inputType)
        {
            switch (inputType)
            {
                case AnyInputState.InputDeviceType.Paddle:
                    return "Paddle";
                case AnyInputState.InputDeviceType.Keyboard1:
                    return "Arrows+Space+Ctrl";
            }
            return "N/A";
        }
        void ChangePlayerIndex(IndexedMenuItem menuItem)
        {
            int index = (int)menuItem.Index;
            if (AnyInputState.InputDeviceTypes[index] == AnyInputState.InputDeviceType.Paddle)
                AnyInputState.InputDeviceTypes[index] = AnyInputState.InputDeviceType.Keyboard1;
            else
                AnyInputState.InputDeviceTypes[index] = AnyInputState.InputDeviceType.Paddle;
            menuItem.Text = string.Format("Player {0} Input: {1}", index + 1, InputToString(AnyInputState.InputDeviceTypes[index]));
        }

        void ChangePlayerInput(object sender, EventArgs e)
        {
            ChangePlayerIndex((IndexedMenuItem)sender);
        }
    }
}
